﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevComponents.AdvTree;
using SyncLib;
using System.Data;
using AppCommon.SyncConfigDSTableAdapters;
using AppCommon;

namespace SyncConfig
{
    public class AdvTreeManipulation
    {

        public const int EnableScopeIndexKey = 2;
        public const int DisableScopeIndexKey = 3;
        public const int TableIndexKey = 4;
        public const int DepenIndexKey = 5;
        public const int FileIndexKey = 7;
        public const int ClientIndexKey = 7;
        public const int HistoryIndexKey = 9;

        public const string CommonScopeName = "CommonScope";
        public const string PrivateScopeName = "PrivateScope";
        public const string SyncScopeHistoryName = "SyncScopeHistory";
        public const string DependencyTableName = "DependencyTable";
        public const string TableOfScopeName = "TableOfScope";
        public const string ClientListName = "ClientsNode";
        public const string SummaryHistoryNodeName = "SummaryHistoryNode";
        public const string ServerVariableNodeName = "ServerVariable";

        public TableOfScopeTableAdapter AdapterTableOfScope;
        public ScopeInfoExTableAdapter AdapterScopeInfoEx;
        public DependencyTableTableAdapter AdapterDependencyTable;
        public ClientTableAdapter AdapterClientTable;
        public ClientConfigTableAdapter AdapterClientConfigTable;
        public Guid ClientId { get; set; }
        public AdvTree Tree
        {
            get;
            set;
        }
        public SyncConfigDS dsSyncConfig { get; set; }
        Dictionary<Guid, Node> ClientNodes;

        public AdvTreeManipulation(DevComponents.AdvTree.AdvTree tree)
        {
            dsSyncConfig = new SyncConfigDS();
            Tree = tree;
            AdapterTableOfScope = new TableOfScopeTableAdapter();
            AdapterTableOfScope.Connection.ConnectionString = CommonVariable.SqlConnectionString;

            AdapterScopeInfoEx = new ScopeInfoExTableAdapter();
            AdapterScopeInfoEx.Connection.ConnectionString = CommonVariable.SqlConnectionString;

            AdapterDependencyTable = new DependencyTableTableAdapter();
            AdapterDependencyTable.Connection.ConnectionString = CommonVariable.SqlConnectionString;

            AdapterClientTable = new ClientTableAdapter();
            AdapterClientTable.Connection.ConnectionString = CommonVariable.SqlConnectionString;

            AdapterClientConfigTable = new ClientConfigTableAdapter();
            AdapterClientConfigTable.Connection.ConnectionString = CommonVariable.SqlConnectionString;
            AdapterScopeInfoEx.ClearBeforeFill = false;
            AdapterTableOfScope.ClearBeforeFill = false;
            ClientNodes = new Dictionary<Guid, Node>();
        }

        public void BuildScopeNode(Node parrent)
        {
            parrent.Nodes.Clear();
            string rowFilter = "";
            if (parrent.Name == CommonScopeName)
            {
                rowFilter = "IsCommon=1";
            }
            else
                rowFilter = "IsCommon=0";
            rowFilter = "";
            DataView scopeView = new DataView(dsSyncConfig.ScopeInfoEx, rowFilter, "Ordinal", DataViewRowState.CurrentRows);

            foreach (DataRowView rowview in scopeView)
            {
                SyncConfigDS.ScopeInfoExRow scopeRow = rowview.Row as SyncConfigDS.ScopeInfoExRow;
                Node scopeNode = CreateSingleScopeNode(null, scopeRow);
                //if (!scopeRow.ClientID.Equals(Guid.Empty))
                //{
                //    if (!ClientId.Equals(Guid.Empty) && !ClientId.Equals(scopeRow.ClientID))
                //    {
                //        continue;
                //    }
                //    if (ClientNodes.ContainsKey(scopeRow.ClientID))
                //    {
                //        ClientNodes[scopeRow.ClientID].Nodes.Add(scopeNode);
                //    }
                //    else
                //    {
                //        Node clienNode = CreateClientNode(dsSyncConfig.Client.FindByClientId(scopeRow.ClientID));
                //        //ClientNodes.Add(scopeRow.ClientID, clienNode);
                //        parrent.Nodes.Add(clienNode);
                //        clienNode.Nodes.Add(scopeNode);

                //    }

                //}
                //else
                parrent.Nodes.Add(scopeNode);
            }
        }
        public Node GetClientNode(Guid clientId)
        {
            if (!ClientNodes.ContainsKey(clientId))
                ClientNodes.Add(clientId, CreateClientNode(dsSyncConfig.Client.FindByClientId(clientId)));
            return ClientNodes[clientId];
        }
        Node CreateClientNode(SyncConfigDS.ClientRow row)
        {
            Node node = new Node();
            node.Text = row.ClientName;
            node.Tooltip = row.Des;
            node.Name = row.ClientName;
            node.ImageIndex = ClientIndexKey;
            return node;
        }

        public Node CreateSingleScopeNode(Node existsNode, SyncConfigDS.ScopeInfoExRow scopeRow)
        {
            if (existsNode == null)
                existsNode = new Node();
            else
                existsNode.Nodes.Clear();

            existsNode.Text = scopeRow.ScopeName;
            // existsNode.Tooltip = (scopeRow.IsCommentNull() ? scopeRow.ScopeName : scopeRow.Comment) + (scopeRow.Enable ? " [Enabled]" : " [Disabled]");
            existsNode.ImageIndex = EnableScopeIndexKey;//scopeRow.Enable ? EnableScopeIndexKey : DisableScopeIndexKey;
            existsNode.Name = scopeRow.ScopeName;
            existsNode.Tag = scopeRow;
            BuildTableOfScopeNodes(existsNode, scopeRow);
            Node his = new Node();
            his.Text = "Lịch sử ";
            his.Name = SyncScopeHistoryName;
            his.ImageIndex = HistoryIndexKey;
            existsNode.Nodes.Add(his);
            return existsNode;
        }
        public void BuildTableOfScopeNodes(Node scopeNode, SyncConfigDS.ScopeInfoExRow scopeRow)
        {
            SyncConfigDS.TableOfScopeRow[] rows = (SyncConfigDS.TableOfScopeRow[])dsSyncConfig.TableOfScope.Select("ScopeExId='" + scopeRow.ScopeExId.ToString() + "'");
            Node nodeTables = new Node();
            nodeTables.Text = "Tables";
            nodeTables.Tooltip = "Tables of " + scopeRow.ScopeName;
            nodeTables.ImageIndex = 0;
            nodeTables.Name = TableOfScopeName;
            nodeTables.ImageExpandedIndex = 1;
            scopeNode.Nodes.Add(nodeTables);
            foreach (SyncConfigDS.TableOfScopeRow row in rows)
            {
                Node node = CreateSingleTableOfScopeNode(null, row);
                nodeTables.Nodes.Add(node);
            }
        }
        public Node CreateSingleTableOfScopeNode(Node existNode, SyncConfigDS.TableOfScopeRow row)
        {
            if (existNode == null)
                existNode = new Node();
            else
                existNode.Nodes.Clear();
            existNode.Text = row.TableName;
            existNode.Tag = row;
            existNode.ImageIndex = TableIndexKey;
            existNode.Name = row.TableName;
            existNode.Nodes.Add(BuildDependencyNode(null, row));
            return existNode;
        }

        public Node BuildDependencyNode(Node node, SyncConfigDS.TableOfScopeRow tableOfScopeRow)
        {
            SyncConfigDS.DependencyTableRow[] rows = (SyncConfigDS.DependencyTableRow[])dsSyncConfig.DependencyTable.Select("TableID='" + tableOfScopeRow.TableId.ToString() + "'");
            if (node == null)
                node = new Node();
            else
                node.Nodes.Clear();

            node.Tooltip = "Dependency Tables of " + tableOfScopeRow.TableName;
            node.Text = "Children Tables";
            node.ImageIndex = 0;
            node.Name = DependencyTableName;
            node.ImageExpandedIndex = 1;
            foreach (SyncConfigDS.DependencyTableRow sub in rows)
                node.Nodes.Add(CreateSingleDepenNode(sub));
            return node;
        }
        public Node CreateSingleDepenNode(SyncConfigDS.DependencyTableRow row)
        {
            Node node = new Node();
            node.Text = row.TableName;
            node.Name = row.TableName;
            node.ImageIndex = DepenIndexKey;
            node.Tag = row;
            return node;
        }
        public void RefreshNode(Node node)
        {

            if (node.Tag == null)
            {
                switch (node.Name)
                {
                    case AdvTreeManipulation.CommonScopeName:
                        BuildScopeNode(node);
                        break;
                    case AdvTreeManipulation.DependencyTableName:
                        break;
                    case AdvTreeManipulation.PrivateScopeName:
                        BuildScopeNode(node);
                        break;
                    case AdvTreeManipulation.SyncScopeHistoryName:
                        break;
                    case AdvTreeManipulation.TableOfScopeName:
                        break;
                }



            }
        }
        public void LoadData()
        {
            ClientNodes = new Dictionary<Guid, Node>();
            dsSyncConfig.Clear();
            AdapterClientTable.Fill(dsSyncConfig.Client);
            SyncConfigDS.ClientRow client = dsSyncConfig.Client.NewClientRow();
            client.ClientId = Guid.Empty;
            client.ClientName = "Chọn máy trạm";
            dsSyncConfig.Client.Rows.InsertAt(client, 0);

            AdapterScopeInfoEx.Fill(dsSyncConfig.ScopeInfoEx);
            AdapterTableOfScope.Fill(dsSyncConfig.TableOfScope);
            AdapterDependencyTable.Fill(dsSyncConfig.DependencyTable);
            AdapterClientConfigTable.SelectAll(dsSyncConfig.ClientConfig);

            dsSyncConfig.AcceptChanges();
        }
        public void BuildTree()
        {


           
            Node commonScopeNode = new Node("Scope chung");
            commonScopeNode.ImageIndex = 0;
            commonScopeNode.ImageExpandedIndex = 1;
            commonScopeNode.Name = CommonScopeName;
            RefreshNode(commonScopeNode); 
           
            Node historyNode = new Node("Lịch sử");
            historyNode.ImageIndex = HistoryIndexKey;            
            historyNode.Name = SummaryHistoryNodeName;            
            Node clientNode = new Node("Máy trạm");
            clientNode.ImageIndex = ClientIndexKey;            
            clientNode.Name = ClientListName;

            Node serverVariable = new Node("Biến hệ thống");
            serverVariable.ImageIndex = 0;
            serverVariable.Name = ServerVariableNodeName;
            

            Tree.Nodes.Add(commonScopeNode);
            Tree.Nodes.Add(clientNode);
            Tree.Nodes.Add(historyNode);
            Tree.Nodes.Add(serverVariable);



        }
    }
}
